Skip to content

drm/apple: v26 link: fix READY/callback race, pump busy-spin, and NULL buffer deref - #4777

Open
RyanTheTide wants to merge 1 commit into
mainfrom
fix/drm-apple-v26-link-handshake-pump-safety
Open

RyanTheTide wants to merge 1 commit into
mainfrom
fix/drm-apple-v26-link-handshake-pump-safety

Conversation

@RyanTheTide

Copy link
Copy Markdown
Contributor

What: Fix four transport bugs in the J613/25G83 DCPLink RPC implementation (drivers/gpu/drm/apple/iomfb_v26_6_link.c): an atomic handshake race between READY and immediate firmware callbacks (plus pre-allocation receive guards), an orphaned callback entry after a failed dcp_v26_link_pump() that caused a 5-second 100% CPU busy-spin in dcp_v26_link_call(), NULL input/output buffer dereferences in dcp_v26_link_call(), and overflow/unchecked message encoding in link_submit().

Tested: ARM64 QEMU KUnit (dcp-v26-link, 9/9 passed under both -smp 2 and -smp 1 with panic_on_warn=1, KASAN, UBSAN, PROVE_LOCKING, FORTIFY_SOURCE, WERROR), W=1 C=1 KCFLAGS="-Wshadow" with KUNIT=y and KUNIT=n, mixed DRM_APPLE=y + KUNIT=m build, scripts/kernel-doc -Wall -none, and scripts/checkpatch.pl --strict --git origin/main..HEAD (0 errors, 0 warnings, 0 checks). Negative-control KUnit runs on unmodified origin/main reproduced all four defects (including the 5000 ms busy-spin and KASAN NULL pointer dereference panic).

Risk: two-way · scoped to drivers/gpu/drm/apple/iomfb_v26_6_link.c and its KUnit suite (iomfb_v26_6_link_test.c); cleanly revertible with git revert 6dc156be3bcc.

Not yet: On-device hardware validation row on a J613/25G83 DCP target (queued in HWROWS-QUEUE.md).

Details
  1. READY-to-callback race and pre-alloc receive safety (dcp_v26_link_start, dcp_v26_link_receive):

    • dcp_v26_link_receive() previously set link->ready_result = 0 and called complete(&link->ready) without setting link->enabled = true under link->lock. Any firmware callback arriving before dcp_v26_link_start() woke and acquired link->lock hit the !link->enabled branch, overwrote ready_result with -EPROTO, dropped the callback, and killed the link.
    • Conversely, a bad ready message did not fail the link, so a subsequent READY before dcp_v26_link_start() woke overwrote -EPROTO with 0.
    • dcp_v26_link_receive() now ignores messages while !link->rpc, atomically sets link->enabled = true on DCP_V26_LINK_READY_MSG, and calls link_fail(link) on a bad handshake message. dcp_v26_link_start() publishes and clears link->rpc under link->lock, preserves -EPROTO on bad handshake, and clears link->enabled on failure/timeout.
  2. Pump failure orphan and 5-second busy-spin (dcp_v26_link_pump, dcp_v26_link_call):

    • When link_callback() failed in dcp_v26_link_pump(), the dequeued event was freed without failing the link or clearing link->callbacks[], leaving event_count == 0 with callback_count > 0. Subsequent dcp_v26_link_call() calls retried -EBUSY in a tight loop for the full 5-second DCP_V26_CALL_TIMEOUT.
    • dcp_v26_link_pump() now fails the link on link_callback() error (an unanswered firmware callback wedges firmware), and dcp_v26_link_call() only retries -EBUSY when READ_ONCE(link->event_count) is non-zero.
  3. NULL buffer dereferences (dcp_v26_link_call):

    • Reject (input_size && !input) || (output_size && !output) with -EINVAL before memcpy().
  4. Overflow-safe capacity check and message encoding validation (link_submit):

    • Replace offset + size > layout.capacity with offset > layout.capacity || size > layout.capacity - offset, and check apple_dcp_link_rpc_message() / apple_dcp_link_stream_message() return values before mutating link->rpc or link->calls[nested].
Evidence

Negative controls on unmodified origin/main (184d0e40c216)

# dcp_v26_link_ready_callback_race_test: ASSERTION FAILED
Expected dcp_v26_link_start(&ctx->link) == 0, but
    dcp_v26_link_start(&ctx->link) == -71 (0xffffffffffffffb9)
not ok 1 dcp_v26_link_ready_callback_race_test

# dcp_v26_link_bad_ready_sticks_test: EXPECTATION FAILED
Expected completion_done(&ctx->link.ready) to be false, but is true
Expected dcp_v26_link_start(&ctx->link) == -71, but
    dcp_v26_link_start(&ctx->link) == 0 (0x0)
not ok 2 dcp_v26_link_bad_ready_sticks_test

# dcp_v26_link_pump_failure_no_spin_test: EXPECTATION FAILED
Expected dcp_v26_link_failed(&ctx->link) to be true, but is false
Expected dcp_v26_link_call(...) == -5, but == -16 (0xfffffffffffffff0)
Expected jiffies_to_msecs(jiffies - start) < 1000U, but == 5000 (0x1388)
Expected jiffies_to_msecs(jiffies - start) < 1000U, but == 5000 (0x1388)
not ok 8 dcp_v26_link_pump_failure_no_spin_test

BUG: KASAN: null-ptr-deref in dcp_v26_link_call+0x170/0x1034
Read of size 4 at addr 0000000000000000 by task kunit_try_catch/51
Kernel panic - not syncing: KASAN: panic_on_warn set ...

Post-fix verification (6dc156be3bcc8cd45bafdcb22244471614628e4e)

  • ARM64 QEMU KUnit SMP (-smp 2, panic_on_warn=1, KASAN, UBSAN, PROVE_LOCKING, FORTIFY_SOURCE, WERROR): Testing complete. Ran 9 tests: passed: 9
  • ARM64 QEMU KUnit UP (-smp 1, panic_on_warn=1): Testing complete. Ran 9 tests: passed: 9
  • ARCH=arm64 LLVM=1 W=1 C=1 KCFLAGS="-Wshadow" (KUNIT=y and KUNIT=n): 0 warnings in modified code (only pre-existing upstream include/linux/wait.h:306 explicit shadow in wait_event_timeout)
  • Mixed config (DRM_APPLE=y, KUNIT=m, DRM_APPLE_KUNIT_TEST=m): builds cleanly with zero modular kunit.ko symbol references
  • scripts/kernel-doc -Wall -none: 0 warnings
  • scripts/checkpatch.pl --strict --git origin/main..HEAD: total: 0 errors, 0 warnings, 0 checks, 315 lines checked

…L buffer deref

Fix four transport bugs in the J613/25G83 DCPLink RPC implementation:

1. Handshake READY-to-callback race and pre-alloc receive handling:
   dcp_v26_link_receive() previously set ready_result = 0 and completed
   link->ready without setting link->enabled = true under link->lock.
   Any firmware callback arriving before dcp_v26_link_start() woke and
   acquired link->lock re-entered the !link->enabled path, overwrote
   ready_result with -EPROTO, dropped the callback, and killed the link.
   Conversely, a malformed ready message did not fail the link, allowing
   a subsequent READY message to overwrite -EPROTO with 0. Also ignore
   messages arriving while link->rpc is NULL, and publish/clear link->rpc
   under link->lock.

2. Pump failure orphan / 5-second CPU busy-spin:
   When link_callback() failed in dcp_v26_link_pump(), the event was
   freed without removing the callback entry or failing the link. Any
   later dcp_v26_link_call() saw event_count == 0 and callback_count > 0,
   causing link_submit() to return -EBUSY and retry in a tight busy-spin
   for the entire 5-second call timeout. Fail the link when
   dcp_v26_link_pump() fails a callback, and only retry -EBUSY when
   event_count is non-zero.

3. NULL input/output buffer dereference in dcp_v26_link_call():
   Reject non-zero input_size/output_size with a NULL pointer before
   copying into or out of the RPC packet.

4. Overflow-safe bounds check and checked message encoding in
   link_submit():
   Avoid offset + size addition overflow and validate
   apple_dcp_link_rpc_message() / apple_dcp_link_stream_message() return
   values before mutating link->rpc or link->calls[nested].

Signed-off-by: Ryan Murray <ryan@aurorasilicon.org>
@RyanTheTide
RyanTheTide requested review from a team as code owners December 6, 2026 10:17
@RyanTheTide

Copy link
Copy Markdown
Contributor Author

Core review at 6dc156be3bcc (drm/apple: v26 link: fix READY/callback race, pump busy-spin, and NULL buffer deref): PASS (no blockers).

Checked

  • READY/callback race & lifecycle (drivers/gpu/drm/apple/iomfb_v26_6_link.c): dcp_v26_link_receive() executes in RTKit ordered workqueue context (not hardirq), so mutex_lock(&link->lock) and GFP_KERNEL are valid. dcp_v26_link_start() initializes the RPC buffers in locals before publishing link->rpc/rpc_dva/enabled = false/ready_result = -ETIMEDOUT under link->lock and clears them under lock before freeing on failure. dcp_v26_link_receive() ignores frames when link->failed || !link->rpc, records ready_result = 0; enabled = true; atomically under lock before complete_all(&link->ready_comp), and preserves prior failure status.
  • Pump -EBUSY busy-spin: bounded drain/backoff loop exits cleanly on callback failure without spinning or missing wakeups (atomic event counter incremented under lock so concurrent callbacks trigger goto retry).
  • NULL buffer validation & arithmetic: rejects non-zero lengths with NULL input/output pointers while allowing valid zero-length calls, and checks layout capacity arithmetic against integer overflow before mutating RPC state.
  • Tests & hygiene: 4 new KUnit tests cover the race, pump, and NULL-buffer paths; DCO Signed-off-by and pr-text clean; no SoC/board gates.

Pending CI completion + exact-head hardware row (will batch into the next kernel cloud-build once lbatch1 finishes).

@github-actions github-actions Bot added area:display Area from CODEOWNERS (bot) door:two-way Revertible or not, from Merge risk (bot) needs-hw-row No hardware row at the PR head (bot) needs-review No approving review from Core or Omarchy Mac yet (bot) labels Dec 6, 2026
@github-actions

github-actions Bot commented Dec 6, 2026 •

Copy link
Copy Markdown

Merge readiness at 6dc156be: not ready yet

Check Detail
CI at head ok 9 checks passed
Review ok approved by aurora-silicon-review, satchlj
Requested changes ok none
Hardware row at head no no line with the head SHA 6dc156be, a Mac and a result in the PR body or a comment
Conflicts ok none
PR text ok Risk: two-way · scoped to drivers/gpu/drm/apple/iomfbv266link.c and its KUnit suite (iomfbv266linktest.c); cleanly revertible with git revert 6dc156be3bcc.

Areas: area:display

Updated by pr-readiness on pushes, edits, reviews and comments, and on a schedule. Rules: merge policy · labels.

@satchlj satchlj left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed at 6dc156be3bcc: setting link->enabled = true under link->lock inside dcp_v26_link_receive() upon DCP_V26_LINK_READY_MSG closes the READY-vs-first-callback race cleanly while preserving -EPROTO on unexpected handshake messages; checking apple_dcp_link_{rpc,stream}_message() errors and overflow-safe layout.capacity bounds before writing shared RPC memory prevents partial submits; dcp_v26_link_pump() fails the link on callback errors; and dcp_v26_link_call() guards NULL non-zero buffers and gates -EBUSY retries on READ_ONCE(link->event_count) to avoid busy-spinning. Code-review only (not tested on hardware).

@github-actions github-actions Bot removed the needs-review No approving review from Core or Omarchy Mac yet (bot) label Dec 6, 2026

@aurora-silicon-review aurora-silicon-review Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: MERGE at 6dc156b

Checked:

  • Full adversarial audit of drivers/gpu/drm/apple/iomfb_v26_6_link.c and the 4 new KUnit tests at 6dc156be3bcc: dcp_v26_link_receive() executes on the RTKit ordered workqueue (valid mutex/GFP_KERNEL), dcp_v26_link_start() publishes rpc/rpc_dva/enabled/ready_result under link->lock and clears under lock on error, pump -EBUSY loop bounds retry without busy-spinning or missed wakeups, NULL non-zero buffers are rejected cleanly while valid zero-length calls remain allowed, and layout capacity arithmetic is overflow-checked.
  • DCO and pr-text clean; no SoC/board gates. Pending CI + exact-head hardware row.

@DjDeveloperr

Copy link
Copy Markdown
Contributor

Hardware check (batched exact-head trial on J516s / M3 Pro T6030):

  • Head: 6dc156be3bcc8cd45bafdcb22244471614628e4e (combined with linux@main 47bb1e8d1dd9 + #4836 + #4790; touched files drivers/gpu/drm/apple/iomfb_v26_6_link.c and iomfb_v26_6_link_test.c verified byte-for-byte identical to PR head)
  • Kernel: 7.3.0-j516s-m3row (aarch64, 16 KiB pages, boot_id=e88ba600-0521-4e33-878d-61bb153cafa7)
  • Result: PASS (bare-metal ARM64 KUnit + live appledrm boot regression)
    • dcp-v26-link: 9/9 pass (dcp_v26_link_multi_callback_fifo_test, dcp_v26_link_nested_call_side_test, dcp_v26_link_start_cleanup_test, dcp_v26_link_callback_depth_limit_test, dcp_v26_link_set_idle_work_null_test, dcp_v26_link_ready_callback_race_test, dcp_v26_link_bad_ready_sticks_test, dcp_v26_link_pump_failure_no_spin_test, dcp_v26_link_call_null_buf_test)
    • Live J516s appledrm regression: card1-eDP-1 connected at 3456x2234, /dev/dri/card0, card1, renderD128 active, 0 dcp-v26-link or appledrm errors
  • Scope note: J516s boots on macOS 27.2 (iomfb_v27), not J613/25G83 (iomfb_v26_6).

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:display Area from CODEOWNERS (bot) door:two-way Revertible or not, from Merge risk (bot) needs-hw-row No hardware row at the PR head (bot)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants